package com.wangbo.mvc;

import cn.hutool.crypto.digest.DigestUtil;
import com.wangbo.test.util.JacksonUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Map;

@RestController
public class TestController {

    /**
     * Postman Body :
     * {
     *     "name": "张三",
     *     "age": 20,
     *     "gender": 男,
     *     "createTime":"2021-08-16"
     * }
     *
     * Response:
     * {"age":20,"name":"张三","gender":"男","createTime":"2021-08-16"}
     */
    @PostMapping("/enumTest")
    public String enumTest(@RequestBody UserParam param) {
        return JacksonUtil.toJson(param);
    }

    /**
     * 测试 MultipartFile md5码
     */
    @ApiOperation(tags = {"文件"}, value = "MultipartFile MD5")
    @PostMapping("/media")
    public String upload(@RequestPart MultipartFile file) throws IOException {
        String md5Hex1 = "", md5Hex2 = "";
        md5Hex1 = DigestUtil.md5Hex(file.getBytes());
        md5Hex2 = DigestUtil.md5Hex(new File("D:\\temp\\a.jpg"));
        return String.format("MultipartFile: %s%nFile: %s%n", md5Hex1, md5Hex2);
    }

    @PostMapping("/receive")
    public void receive(Map map) {
        System.out.println(map);
    }
}
